package com.demo.plugin.run;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;


import java.io.*;

@Mojo(name = "build")
public class DockfireMojo extends AbstractMojo {

    /**
     * "expression"：从系统属性读取这几个字段；"@readonly"：不允许用户进行配置
     * 项目基础目录
     *
     * @parameter expression="${project.basedir}"
     * @required
     * @readonly
     */
    @Parameter(property = "basedir")
    private String basedir;

    @Parameter(property = "volume")
    private String volume;

    @Parameter(property = "jarName")
    private String jarName;

    /**
     * 所属模块
     */
    @Parameter(property = "modelName")
    private String modelName;

    String line = "\r\n";

    //    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        //获取当前项目的根路径
        System.out.println("插件执行开始：---------------------");
        try {
            //获取项目根路径
            System.out.println("baseDir=" + basedir);
            //创建dockfire文件
            createDockerFile(basedir + "/docker/");
            //创建properties文件
            createPropertiesFile(basedir + "/main/resources");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建properties
     *
     * @param path 创建文件路径
     */
    private void createPropertiesFile(String path) throws IOException {
        /**
         * 创建peroperties文件
         */
        File propertiesFile = new File(path, "project.properties");
        //创建文件路径
        createFilePath(propertiesFile);
        //创建文件内容
        createPropertiesInfo(propertiesFile);
    }

    /**
     * 创建properties文件
     *
     * @param propertiesFile
     */
    private void createPropertiesInfo(File propertiesFile) throws IOException {
        //对原文件进行覆盖
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(propertiesFile, false);
            bw = new BufferedWriter(fw);
            bw.write("model=" + modelName + line);
        } catch (IOException e) {
            throw e;
        } finally {
            bw.flush();
            bw.close();
            fw.close();
        }
    }

    /**
     * 创建dockerFile
     *
     * @param filePath 创建dockerPath
     */
    private void createDockerFile(String filePath) throws IOException {
        //创建docker文件
        File dockerFile = new File(filePath, "Dockerfile");
        //创建文件路径
        createFilePath(dockerFile);
        createDockfireInfo(dockerFile);
        //创建properties文件
    }


    /**
     * 写入docker文件内容
     *
     * @param dockerFile
     * @throws IOException
     */
    private void createDockfireInfo(File dockerFile) throws IOException {
        //对原文件进行覆盖
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(dockerFile, false);
            bw = new BufferedWriter(fw);
            /**
             * dockerFile内容：
             * 首先填写 FROM openjdk:8-jre-alpine  images 基础镜像
             */

            bw.write("FROM openjdk:8-jre-alpine" + line);
            bw.write(line);

            /**
             * 设置env
             * 设置环境变量
             */

            String dev = System.getProperty("env");
            System.out.println("环境为：" + dev);
            if (dev == null) {
                bw.write("ENV PROFILE dev" + line);
            } else {
                bw.write("ENV PROFILE " + dev + line);
            }
            bw.write(line);

            /**
             * 设置加载的jar包
             * ADD *.jar /app.jar 将所有的jar包添加
             */
            bw.write("ADD " + jarName + " /app.jar" + line);
            bw.write(line);

            /**
             * 设置需要额外加载的路径打包到docke
             * VOLUME /home/work/springDemo
             */
            if (!(volume == "" || volume == null)) {
                bw.write("VOLUME " + volume + line);
                bw.write(line);
            }

            /**
             * 添加run
             * RUN sh -c 'touch /app.jar'
             *
             */
            bw.write("RUN sh -c 'touch /app.jar'" + line);
            bw.write(line);

            /**
             * 添加ENTRYPOINT
             * ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
             */

            bw.write("ENTRYPOINT [\"java\",\"-Djava.security.egd=file:/dev/./urandom\",\"-jar\",\"/app.jar\"]" + line);
            bw.write(line);
        } catch (IOException e) {
            throw e;
        } finally {
            bw.flush();
            bw.close();
            fw.close();
        }
    }

    /**
     * 判断路径是否存在创建路径
     *
     * @param file 创建file
     */
    private void createFilePath(File file) throws IOException {
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
}
